<?php

namespace App\Http\Controllers\Admin;

use App\Models\Role;
use Illuminate\Http\Request;
//use App\Http\Controllers\Controller;
# 引入用户模型
use App\Model\Admin;
class AdminController extends BaseController
{
    # 用户首页
    public function index(Request $request){
        # dump($request->all());
        $datemax = $request->get("datemax");
        $datemin = $request->get("datemin");
        $username = $request->get("username");
        # 根据传入的数据进行搜索数据
        $data = Admin::with("role")->when($datemax,function ($query) use ($datemax){
            $datemax = date("Y-m-d 00:00:00",strtotime($datemax));
            $query->where("created_at",">=",$datemax);
        })->when($datemin,function ($query) use ($datemin){
            $datemin = date("Y-m-d 23:59:59",strtotime($datemin));
            $query->where("created_at","<=",$datemin);
        })->when($username,function ($query) use ($username){
            $query->where("username","like","%{$username}%");
        })->withTrashed()->orderBy("id","besc")->paginate($this->pagsize);
//        dump($data);die;
        $admin = new Admin();
        # 查询到所有的数据 onlyTrashed-》查询软删除的数据
        # $data = Admin::withTrashed()->orderBy("id","besc")->paginate($this->pagsize);
        return view("admin.admin.index",compact("data","admin"));
    }
    # 添加页面展示
    public function insert(){
        $data = Role::get();
        return view("admin.admin.insert",compact("data"));
    }
    # 接收添加数据处理
    public function create(Request $request){
        $data = $this->validate($request,[
            "username" => "required|unique:admins,username",
            "truename" => "required",
            "password" => "required|confirmed",
            "phone" => "required|regex:/^1[3-9]\d{9}$/",
            "email" => "required|email",
            "sex" => "required",
            "role_id" => "required"
        ]);
        $data["password"] = bcrypt($data["password"]);
        Admin::create($data);
        return redirect( route("admin.admin.user"))->with("success","用户添加成功");

    }
    # 修改用户页面展示
    public function edit(int $id){
        $data = Admin::with("role")->find($id);
        $list = Role::get();
        return view("admin.admin.edit",compact("data","list"));
    }
    # 修改数据的处理
    public function update(Request $request,int $id){
        $data = $this->validate($request,[
            "username" => "required|unique:admins,username,".$id,
            "truename" => "required",
            "password" => "nullable|confirmed",
            "phone"    => "required|regex:/^1[3-9]\d{9}$/",
            "email"    => "required|email",
            "sex"      => "required",
            "role_id"  => "required"
        ]);
//        dump($data);
        Admin::where("id",$id)->update($data);
        return redirect( route("admin.admin.user"))->with("success","修改成功");
    }
    # 进行软删除数据
    public function destory(int $id){
//        dump(auth()->user()->id);die;
        if(auth()->user()->id == $id){
            return ["status" => 400,"msg" => "删除失败，不能删除自己"];
        }
        Admin::destroy($id);
        return ["status" => 200,"msg" => "删除成功"];
    }
    # 全部删除
    public function deleteAll(Request $request){
        $id = $request->get("id");
        foreach ($id as $value){
            if(13 == $value){
                return ["status" => 400,"msg" => "删除失败，不能删除自己"];
            }
        }

        Admin::destroy($request->get("id"));
        return ["status"=>200,"msg"=>"删除成功"];
    }
    # 软删除列表
    public function softDeletes(){
        $data = Admin::onlyTrashed()->orderBy("id","besc")->paginate($this->pagsize);
        return view("admin.admin.softDeletes",compact("data"));
    }
    # 真删除
    public function deletes(int $id){
        Admin::where("id",$id)->forceDelete();
        return ["status" => 200,"msg" => "删除成功"];
    }
    # 还原数据
    public function restore(int $id){
        Admin::where("id",$id)->restore();
        return redirect(route("admin.admin.user"))->with("success","数据还原成功");
    }
}
